package com.muyu.cloud.commodity.service.imp;

import com.muyu.cloud.commodity.domain.req.SpecValueAddReq;
import com.muyu.cloud.commodity.mapper.SpecGroupMapper;
import com.muyu.cloud.commodity.mapper.SpecValueMapper;
import com.muyu.cloud.commodity.domain.model.SpecGroupListModel;
import com.muyu.cloud.commodity.domain.req.SpecGroupListReq;
import com.muyu.cloud.commodity.domain.req.SpecGroupSaveReq;
import com.muyu.cloud.commodity.domain.model.SpecGroup;
import com.muyu.cloud.commodity.domain.model.SpecValue;
import com.muyu.cloud.commodity.service.SpecGroupService;
import com.muyu.common.core.web.page.PageQueryModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:602048165@qq.com">60204</a>
 * @version 1.0
 * @description:
 * @date 2025/9/15 21:00
 */
@Service
public class SpecGroupServiceImpl implements SpecGroupService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecValueMapper specValueMapper;

    /**
     * 规格分页列表
     * @param req
     * @return
     */
    @Override
    public PageQueryModel<SpecGroupListModel> pageQuery(SpecGroupListReq req) {
        long total= specGroupMapper.countByQuery(req);
        List<SpecGroupListModel>rows= total>0?specGroupMapper.selectPageByQuery(req): List.of();
        return PageQueryModel.<SpecGroupListModel>builder()
                .total(total)
                .dataList(rows)
                .build();
    }

    /**
     * 规格添加
     * @param req
     */
    @Override
    @Transactional
    public void saveWithValues(SpecGroupSaveReq req) {
        // 1. 保存/更新规格组
        SpecGroup group = new SpecGroup();
        group.setId(req.getId());
        group.setGroupName(req.getGroupName());
        group.setCategoryId(req.getCategoryId());
        group.setSort(req.getSort());
        group.setStatus(req.getStatus());

        Long groupId;
        if (group.getId() == null) {
            // 新增规格组
            specGroupMapper.insertGroupReturnId(group);
            groupId = group.getId();
        } else {
            // 更新规格组
            specGroupMapper.updateById(group);
            groupId = group.getId();

            // 删除该组下的所有规格值
            specValueMapper.deleteByGroupId(groupId);
        }

        // 2. 批量插入规格值
        if (req.getValues() != null && !req.getValues().isEmpty()) {
            List<SpecValue> specValues = new ArrayList<>();
            for (SpecValueAddReq vReq : req.getValues()) {
                SpecValue v = new SpecValue();
                v.setGroupId(groupId);
                v.setValueName(vReq.getValueName());
                v.setColorCode(vReq.getColorCode());
                v.setImage(vReq.getImage());
                v.setSort(vReq.getSort() == null ? 0 : vReq.getSort());
                v.setStatus(vReq.getStatus() == null ? 1 : vReq.getStatus());
                specValues.add(v);
            }
            specValueMapper.insertList(specValues);
        }
    }
}